package com.heyqing.topic.IsHappy_0202;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * ClassName:IsHappy
 * Package:com.heyqing.topic.IsHappy_0202
 * Description:
 *
 * @Date:2024/9/21
 * @Author:Heyqing
 */
public class IsHappy {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n < Integer.MAX_VALUE) {
            int temp = 0;
            while (n > 0) {
                int last = n % 10;
                temp += last * last;
                n /= 10;
            }
            int size = set.size();
            set.add(temp);
            if (size - set.size() == 0) return false;
            if (temp == 1) return true;
            n = temp;
        }
        return false;
    }


    public static void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("n:");
        int n = sc.nextInt();
        IsHappy ih = new IsHappy();
        boolean happy = ih.isHappy(n);
        System.out.println(happy);
    }
}
